package com.hy.main.demo.rabbitmq;

import com.rabbitmq.client.CancelCallback;
import com.rabbitmq.client.Channel;
import com.rabbitmq.client.DeliverCallback;


/**
 * @Author yang DongYong
 * @Date 2022/5/11 - 05 - 11 - 1:32
 * @Description: com.hy.main.demo.rabbitmq
 * @version: 1.0
 * 订阅
 */
public class ReceiveLogs01 {
    public static final String EXCHANGE_NAME = "logs";

    public static void main(String[] args) throws Exception {
        Channel channel = RabbitMqUtils.getChannel();
        // 声明一个交换机
        channel.exchangeDeclare(EXCHANGE_NAME, "fanout");
        // 声明一个临时队列 队列名称是随机的 当断开与队列连接时候就会自动删除
        String queueName = channel.queueDeclare().getQueue();
        channel.queueBind(queueName, EXCHANGE_NAME, "");
        System.out.println("ReceiveLogs01等待接收消息....");

        DeliverCallback deliverCallback = (consumerTag, message) ->
                System.out.println("ReceiveLogs01接收到的消息:" + new String(message.getBody()));
        CancelCallback cancelCallback = System.out::println;

        channel.basicConsume(queueName, true,deliverCallback,cancelCallback );


    }
}

 class ReceiveLogs02 {
    public static final String EXCHANGE_NAME = "logs";

    public static void main(String[] args) throws Exception {
        Channel channel = RabbitMqUtils.getChannel();
        // 声明一个交换机
        channel.exchangeDeclare(EXCHANGE_NAME, "fanout");
        // 声明一个临时队列 队列名称是随机的 当断开与队列连接时候就会自动删除
        String queueName = channel.queueDeclare().getQueue();
        channel.queueBind(queueName, EXCHANGE_NAME, "");
        System.out.println("ReceiveLogs02等待接收消息....");

        DeliverCallback deliverCallback = (consumerTag, message) -> System.out.println("ReceiveLogs02接收到的消息:" + new String(message.getBody()));
        CancelCallback cancelCallback = System.out::println;

        channel.basicConsume(queueName, true,deliverCallback,cancelCallback );


    }
}
